﻿using CasignDesignGA.Casing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CasignDesignGA.Engine.Selection
{
    class RouletteSelectionAlgorithm : ISelectionAlgorithm
    {
        private static RouletteSelectionAlgorithm instance;
        private RouletteSelectionAlgorithm()
        {
           
        }
        public static RouletteSelectionAlgorithm Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RouletteSelectionAlgorithm();
                }
                return instance;
            }
        }
        public Population.Population selectPopulation(Population.Population individuals, int nIndividuals)
        {
            Population.Population selectedIndividuals = new Population.Population();

            int nRoulette = nIndividuals ;
            Random rand = new Random(DateTime.Now.Millisecond);
            /*Roulette part*/
            for (int i = 0; i < nRoulette; i++)
            {
                double randIndex = rand.NextDouble() * individuals.count();
                int index = Convert.ToInt32(randIndex);
                ISelectable selectedRouletteIndividual = individuals.getIndividualIndex(index);
                if (selectedRouletteIndividual != null && !selectedIndividuals.Individuals.Contains(selectedRouletteIndividual))
                {
                    selectedIndividuals.Add(selectedRouletteIndividual);
                }
                

            }

        

            return selectedIndividuals;
        }
    }
}
